<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;

class Select extends Common
{

    //根据分组查竞品
    public function competitor(){
        $catid = $this->request->get('catid');
        echo "<option value=''>请选择</option>";
        //查询该分类下的所有竞品
        $data = Db::table('competitor')->where('category_id',$catid)->field('id,c_name')->select();
        foreach($data as $k=>$v){
            echo "<option value='{$v['id']}'>{$v['c_name']}</option>";
        }
    }
    //获取营销省份
    public function provice(){
        $catid = $this->request->param('id');
        echo "<option value=''>请选择</option>";
        //查询该分类下的所有竞品
        $data = Db::table('cg_market_provice')->where('zoning',$catid)->field('id,title')->select();
        foreach($data as $k=>$v){
            echo "<option value='{$v['id']}'>{$v['title']}</option>";
        }
    }
    //获取营销城市
    public function city(){
        $catid = $this->request->param('id');
        echo "<option value=''>请选择</option>";
        //查询该分类下的所有竞品
        $data = Db::table('cg_market_city')->where('provice',$catid)->field('id,title')->select();
        foreach($data as $k=>$v){
            echo "<option value='{$v['id']}'>{$v['title']}</option>";
        }
    }

    //获取行政城市
    public function ecity(){
        $catid = $this->request->param('id');
        echo "<option value=''>请选择</option>";
        //查询该分类下的所有竞品
        $data = Db::table('cg_city')->where('provice',$catid)->field('id,title')->select();
        foreach($data as $k=>$v){
            echo "<option value='{$v['id']}'>{$v['title']}</option>";
        }
    }

    //获取行政城市
    public function ecounty(){
        $catid = $this->request->param('id');
        echo "<option value=''>请选择</option>";
        //查询该分类下的所有竞品
        $data = Db::table('cg_county')->where('city',$catid)->field('id,title')->select();
        foreach($data as $k=>$v){
            echo "<option value='{$v['id']}'>{$v['title']}</option>";
        }
    }

    //suggest所有产品
    public function product(){
        $where = [];
        $keyword = $this->request->param('keyword');
        if($keyword){
            $series = Db::table('pro_series')->where('category_id',$keyword)->field('id')->select();
            foreach($series as $k=>$v){
                $series[$k] = $v['id'];
            }
            $series = implode(',',$series);
            $sku = Db::table('pro_sku')->where('series_id',$series)->field('id')->select();
            foreach($sku as $k=>$v){
                $sku[$k] = $v['id'];
            }
            $sku = implode(',',$sku);

            $where['sku_id'] = ['in',$sku];
        }
        $data = Db::table('pro_products')->field('id,pro_name title,unit,pro_no')->where($where)->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //查询所有经销商
    public function jxsjson(){
        $jxs_id = $this->request->param('jxs_id');
        $where = [];
        if($jxs_id){
            $where['id'] = $jxs_id;
        }
        //查询所有省份
        $data = Db::table('cg_jxs')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //查询所有经销商
    public function sysjson(){
       
        $data = Db::table('cg_system')->field('title,id')->where("isdel=0")->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //登陆人能操作的经销商
    public function myjxsjson(){
        $back=$this->my_own_jxs();
        if($back){
            $backlist=implode(',',$back);
            $where="id in ($backlist) and isdel=0";
        }else{
            $where="1<>1";
        }
        $data = Db::table('cg_jxs')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //登陆人能操作的系统
    public function mysysjson(){
        $back=$this->my_own_sys();
        if($back){
            $backlist=implode(',',$back);
            $where="id in ($backlist) and isdel=0";
        }else{
            $where="1<>1";
        }
        $data = Db::table('cg_system')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //登录人能操作的网点
    public function mynetjson(){
        $back=$this->my_own_net();
        if($back){
            $backlist=implode(',',$back);
            $where="id in ($backlist) and isdel=0";
        }else{
            $where="1<>1";
        }
        $data = Db::table('cg_net')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
     public function fenqujson(){
       
        $data = Db::table('cg_market_zoning')->field('title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    public function shengjson(){
        $data = Db::table('cg_market_provice')->field('title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    public function shijson(){
        $data = Db::table('cg_market_city')->field('title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    public function bumenjson(){
        $data = Db::table('department')->where("is_actived=1 and is_deleted=0")->field('de_name as title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    public function yijifeiyongjson(){
        $data = Db::table('cg_feiyongkemu1')->field('title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
     public function erjifeiyongjson(){
        $data = Db::table('cg_feiyongkemu2')->field('title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
     public function sanjifeiyongjson(){
        $data = Db::table('cg_feiyongkemu3')->field('title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //登录人分区
    public function myfenqujson(){
        $back=$this->my_own_fenqu();
        if($back){
            $backlist=implode(',',$back);
            $where="id in ($backlist) and isdel=0";
        }else{
            $where="1<>1";
        }
        $data = Db::table('cg_market_zoning')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
     //登录人身份
    public function myshengjson(){
        $back=$this->my_own_sheng();
        if($back){
            $backlist=implode(',',$back);
            $where="id in ($backlist) and isdel=0";
        }else{
            $where="1<>1";
        }
        $data = Db::table('cg_market_provice')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
     //登录人城市
    public function myshijson(){
        $back=$this->my_own_shi();
        if($back){
            $backlist=implode(',',$back);
            $where="id in ($backlist) and isdel=0";
        }else{
            $where="1<>1";
        }
        $data = Db::table('cg_market_city')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //产品品牌suggest数据
    public function brands(){
        //查询
        $data = Db::table('pro_brands')->field('id,brand_name title')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
     //返回分区经销商树数据
    public function myzonetreeajax(){
        $jxs_id = $this->request->param('jxs_id');

        $arr=$this->my_own_jxs();

        if($arr){
            $str=implode(',',$arr);
            $mwhere="id in ($str) ";
        }else{
            $mwhere="1<>1 ";
        }
        if($jxs_id){
            $data2 = Db::table('cg_jxs')->where(array('id'=>$jxs_id))->field('id,title name')->select();//查经销商
            echo json_encode($data2);
            die;
        }
        $data = Db::table('cg_market_zoning')->where('isdel',0)->field('id,title')->select();//查分区
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['title'];
            //查询省份
            $t = Db::table('cg_market_provice')->where(array('zoning'=>$v['id'],'isdel'=>0))->field('id,title')->select();//查省份
            $data3 = [];
            foreach($t as $kk=>$vv){
                $temp2['name'] = $vv['title'];
                //查城市
                $tt = Db::table('cg_market_city')->where(array('provice'=>$vv['id'],'isdel'=>0))->field('id,title')->select();//查城市
                $data4 = [];
                foreach($tt as $k3=>$v3){
                    $temp3['name'] = $v3['title'];
                    //查经销商
                    $t3 = Db::table('cg_jxs')->where("city={$v3["id"]} and isdel=0 and  ".$mwhere)->field('id,title name')->select();//查经销商
                    $temp3['children'] = $t3;
                    $temp3["chkDisabled"]=true;
                    $temp3["nocheck"]=true;
                    $data4[] = $temp3;
                }
                $temp2['children'] = $data4;
                $temp2["chkDisabled"]=true;
                $temp2["nocheck"]=true;
                $data3[] = $temp2;
            }
            $temp['children'] = $data3;
            $temp["chkDisabled"]=true;
            $temp["nocheck"]=true;
            $data2[] = $temp;
        }

        echo json_encode($data2);
    }
     //返回分区系统树数据
    public function mysystreeajax(){
         $arr=$this->my_own_sys();
        if($arr){
            $str=implode(',',$arr);
            $mwhere="id in ($str) ";
        }else{
            $mwhere="1<>1 ";
        }
       $data=[];
        $res1= Db::table('cg_jxs')->where('isdel=0')->field('title as name,id')->select();
        for($i=0;$i<count($res1);$i++){
             
              $data[]=["id"=>"J".$res1[$i]["id"],"name"=>$res1[$i]["name"],"pId"=>"0","nocheck"=>true];
                
           }
        $res2= Db::table('cg_system')->where("isdel=0 and {$mwhere}")->field('title as name,id,jxs as pid')->select();
          for($i=0;$i<count($res2);$i++){
             
              $data[]=["id"=>$res2[$i]["id"],"name"=>$res2[$i]["name"],"pId"=>"J".$res2[$i]["pid"]];
                
           }
         return json_encode($data);
        //print_r($data);exit;
        //echo json_encode($data2);
    }
    public function mynettree(){
        $arr=$this->my_own_net();
        if($arr){
            $str=implode(',',$arr);
            $mwhere="id in ($str) ";
        }else{
            $mwhere="1<>1 ";
        }
        $data=[];
        $res1= Db::table('cg_jxs')->where('isdel=0')->field('title as name,id')->select();
        for($i=0;$i<count($res1);$i++){
             
              $data[]=["id"=>"J".$res1[$i]["id"],"name"=>$res1[$i]["name"],"pId"=>"0","nocheck"=>true];
                
           }
        $res2= Db::table('cg_system')->where('isdel=0')->field('title as name,id,jxs as pid')->select();
          for($i=0;$i<count($res2);$i++){
             
              $data[]=["id"=>"X".$res2[$i]["id"],"name"=>$res2[$i]["name"],"pId"=>"J".$res2[$i]["pid"],"nocheck"=>true];
                
           }
            $res3= Db::table('cg_net')->where("isdel=0 and {$mwhere}")->field('title as name,id,system_id as pid')->select();
             for($i=0;$i<count($res3);$i++){
             
              $data[]=["id"=>$res3[$i]["id"],"name"=>$res3[$i]["name"],"pId"=>"X".$res3[$i]["pid"]];
                
           }
         return json_encode($data);
    }
    //返回分区经销商树数据
    public function zonetreeajax(){
        $jxs_id = $this->request->param('jxs_id');
        if($jxs_id){
            $data2 = Db::table('cg_jxs')->where(array('id'=>$jxs_id))->field('id,title name')->select();//查经销商
            echo json_encode($data2);
            die;
        }
        $data = Db::table('cg_market_zoning')->where('isdel',0)->field('id,title')->select();//查分区
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['title'];
            //查询省份
            $t = Db::table('cg_market_provice')->where(array('zoning'=>$v['id']))->field('id,title')->select();//查省份
            $data3 = [];
            foreach($t as $kk=>$vv){
                $temp2['name'] = $vv['title'];
                //查城市
                $tt = Db::table('cg_market_city')->where(array('provice'=>$vv['id']))->field('id,title')->select();//查城市
                $data4 = [];
                foreach($tt as $k3=>$v3){
                    $temp3['name'] = $v3['title'];
                    //查经销商
                    $t3 = Db::table('cg_jxs')->where(array('city'=>$v3['id']))->field('id,title name')->select();//查经销商
                    $temp3['children'] = $t3;
                    $temp3["chkDisabled"]=true;
                    $temp3["nocheck"]=true;
                    $data4[] = $temp3;
                }
                $temp2['children'] = $data4;
                $temp2["chkDisabled"]=true;
                $temp2["nocheck"]=true;
                $data3[] = $temp2;
            }
            $temp['children'] = $data3;
            $temp["chkDisabled"]=true;
            $temp["nocheck"]=true;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }
    //返回网点树数据-从分区开始
    public function nettreeajax(){
        $data = Db::table('cg_market_zoning')
            ->where('isdel',0)
            ->field('id,title')
            ->select();//查分区
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['title'];
            //查询省份
            $t = Db::table('cg_market_provice')
                ->where(array('zoning'=>$v['id']))
                ->field('id,title')
                ->select();//查省份
            $data3 = [];
            foreach($t as $kk=>$vv){
                $temp2 = [];
                $temp2['name'] = $vv['title'];
                //查城市
                $tt = Db::table('cg_market_city')
                    ->where(array('provice'=>$vv['id']))
                    ->field('id,title')
                    ->select();//查城市
                $data4 = [];
                foreach($tt as $k3=>$v3){
                    $temp3 = [];
                    $temp3['name'] = $v3['title'];
                    $t3 = Db::table('cg_jxs')
                        ->where(array('city'=>$v3['id']))
                        ->field('id,title')
                        ->select();//查经销商
                    //查系统
                    $data5 = [];
                    foreach($t3 as $k4=>$v4){
                        $temp5 = [];
                        $temp5['name'] = $v4['title'];
                        $t4 = db('cg_system')
                            ->where([
                                'jxs'=>$v4['id']
                            ])
                            ->field('id,title')
                            ->select();//查系统

                        $data6 = [];
                        foreach($t4 as $t4_k =>$t4_v){
                            $temp6 = [];
                            $temp6['name'] = $t4_v['title'];
                            $t5 = db('cg_net')
                                ->where(['system_id'=>$t4_v['id']])
                                ->field('id,title,iskey')
                                ->select();//查网点
                            $data7 = [];
                            foreach ($t5 as $t5_k=>$t5_v){
                                $temp7 = [];
                                $temp7['name'] = $t5_v['title'];
                                $temp7['id'] = $t5_v['id'];
                                if($t5_v['iskey']==1){
                                    $temp7['checked'] = true;
                                }
                                $data7[] = $temp7;
                            }
                            $temp6['children'] = $data7;
                            $temp6["chkDisabled"]=true;
                            $temp6["nocheck"]=true;
                            $data6[] = $temp6;
                        }
//                        echo db('cg_net')->getLastSql();
                        $temp5['children'] = $data6;
                        $temp5["chkDisabled"]=true;
                        $temp5["nocheck"]=true;
                        $data5[] = $temp5;
                    }

                    $temp3['children'] = $data5;
                    $temp3["chkDisabled"]=true;
                    $temp3["nocheck"]=true;
                    $data4[] = $temp3;
                }
                $temp2['children'] = $data4;
                $temp2["chkDisabled"]=true;
                $temp2["nocheck"]=true;
                $data3[] = $temp2;
            }
            $temp['children'] = $data3;
            $temp["chkDisabled"]=true;
            $temp["nocheck"]=true;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }

    //返回品牌数
    public function brandsTree(){
        $data = Db::table('pro_brands')->field('id,brand_name name')->select();
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['name'];
            $temp['id'] = $v['id'];
            $temp["chkDisabled"]=false;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }
    //返回品类树-根据指定品牌
    public function categoryTree(){
        $data = $this->request->get('keyword');
        $where = [];
        if($data){
            $where['brand_id'] = ['in',$data];
        }
        $data = Db::table('pro_category')->field('id,category_name name')->where($where)->select();
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['name'];
            $temp['id'] = $v['id'];
            $temp["chkDisabled"]=false;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }

    //品类树-从品牌开始
    public function categoryTreeAll(){

        $brands = db('pro_brands')
            ->field('id,brand_name name')
            ->select();
        foreach($brands as $k=>$v){
            $data = Db::table('pro_category')
                ->field('id,category_name name')
                ->where('brand_id',$v['id'])
                ->select();
            $brands[$k]['children'] = $data;
        }

        echo json_encode($brands);
    }
    //系列树-从品牌开始
    public function seriesTreeAll(){

        $brands = db('pro_brands')
            ->field('id,brand_name name')
            ->select();
        foreach($brands as $k=>$v){
            $category = Db::table('pro_category')
                ->field('id,category_name name')
                ->where('brand_id',$v['id'])
                ->select();
            foreach($category as $c_k=>$c_v){
                $series = Db::table('pro_series')
                    ->field('id,series_name name')
                    ->where('category_id',$c_v['id'])
                    ->select();
                $category[$c_k]['children'] = $series;
            }
            $brands[$k]['children'] = $category;
        }

        echo json_encode($brands);
    }

    //sku树-从品牌开始
    public function skuTreeAll(){

        $brands = db('pro_brands')
            ->field('id,brand_name name')
            ->select();
        foreach($brands as $k=>$v){
            $category = Db::table('pro_category')
                ->field('id,category_name name')
                ->where('brand_id',$v['id'])
                ->select();
            foreach($category as $c_k=>$c_v){
                $series = Db::table('pro_series')
                    ->field('id,series_name name')
                    ->where('category_id',$c_v['id'])
                    ->select();
                foreach($series as $s_k=>$s_v){
                    $sku = Db::table('pro_sku')
                        ->field('id,sku_name name')
                        ->where('series_id',$s_v['id'])
                        ->select();
                    $series[$s_k]['children'] = $sku;
                }
                $category[$c_k]['children'] = $series;
            }
            $brands[$k]['children'] = $category;
        }

        echo json_encode($brands);
    }
    //产品树-从品牌开始
    public function productTreeAll(){

        $brands = db('pro_brands')
            ->field('id,brand_name name')
            ->select();
        foreach($brands as $k=>$v){
            $category = Db::table('pro_category')
                ->field('id,category_name name')
                ->where('brand_id',$v['id'])
                ->select();
            foreach($category as $c_k=>$c_v){
                $series = Db::table('pro_series')
                    ->field('id,series_name name')
                    ->where('category_id',$c_v['id'])
                    ->select();
                foreach($series as $s_k=>$s_v){
                    $sku = Db::table('pro_sku')
                        ->field('id,sku_name name')
                        ->where('series_id',$s_v['id'])
                        ->select();
                    foreach($sku as $sku_k=>$sku_v){
                        $product = Db::table('pro_products')
                            ->field('id,pro_name name')
                            ->where('sku_id',$sku_v['id'])
                            ->select();
                        $sku[$sku_k]['children'] = $product;
                    }
                    $series[$s_k]['children'] = $sku;
                }
                $category[$c_k]['children'] = $series;
            }
            $brands[$k]['children'] = $category;
        }

        echo json_encode($brands);
    }

    //suggest请求品类
    public function pro_category(){
        $data = $this->request->get('keyword');
        //查询
        $data = Db::table('pro_category')->field('id,category_name title')->where('brand_id','in',$data)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //suggest按照经销商来查找网点
    public function netjson(){
        $data = $this->request->get('keyword');
        $systems = Db::table('cg_system_jxs')->where('jxs_id',$data)->field('system_id')->select();
        foreach($systems as $k=>$v){
            $systems[$k] = $v['system_id'];
        }
        $systems = implode(',',$systems);
        $data = Db::table('cg_net')->field('title,id')->where('system_id','in',$systems)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }


    //suggest查找网点
    public function netjson2(){
        $where = [];
        $jxs_id = $this->request->param('jxs_id');
        if($jxs_id){
            $sys_ids = db('cg_system')->where('jxs',$jxs_id)->column('id');
            $where['system_id'] = ['in',$sys_ids];
        }
        $data = Db::table('cg_net')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //suggest查找品牌
    public function brandjson(){
        $data = Db::table('pro_brands')->field('brand_name title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //suggest查找品类
    public function categoryjson(){
        $data = Db::table('pro_category')->field('category_name title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //suggest查找系列
    public function seriesjson(){
        $data = Db::table('pro_series')->field('series_name title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //suggest查找sku
    public function skujson(){
        $data = Db::table('pro_sku')->field('sku_name title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //suggest查找产品
    public function productjson(){
        $data = Db::table('pro_products')->field('pro_name title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //suggest查找费用计入部门字典
    public function jrbmjson(){
        $data = Db::table('cg_fijiruzd t')->join('department t2','t2.id=t.title')->field('t2.de_name,t.id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
     //suggest查找费用计入部门字典
    public function bmjson(){
        $data = Db::table('department')->field('de_name,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
     //suggest查找考核期
    public function kaoheqijson(){
        $kaohes=db("cg_kaoheqi2 k2")->join("cg_kaoheqi1 k1","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")->order("k2.starttime desc")->field("k2.id,k2.title,c.title as cainian")->select();
        $data=[];
        foreach ($kaohes as $k => $v) {
            # code...
            $data[$k]["id"]=$v["id"];
            $data[$k]["title"]=$v["cainian"].'-'.$v["title"];
        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //suggest查找费用科目3
    public function feiyongkemu3json(){;
        $where = [];
        $data = Db::table('cg_feiyongkemu3')->field('title,id')->where($where)->select();
//        echo Db::table('cg_feiyongkemku3')->getLastSql();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //suggest查找费用科目3
    public function gyssug(){
        $esuseway = $this->request->param('esuseway');
        $where = [];
        if($esuseway){
            $where['esuseway'] = ['like','%"'.$esuseway.'"%'];
        }
        $data = Db::table('gys')->field('gys_name,id')->where($where)->select();
//        echo Db::table('cg_feiyongkemku3')->getLastSql();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //按照经销商返回网点树
    public function netTree(){
        $data = $this->request->param('keyword');
        $where = [];
        if($data){
            $systems = Db::table('cg_system_jxs')->where('jxs_id',$data)->field('system_id')->select();
            foreach($systems as $k=>$v){
                $systems[$k] = $v['system_id'];
            }
            $systems = implode(',',$systems);
            $where['system_id'] = ['in',$systems];
        }
        $data = Db::table('cg_net')->field('id,title name')->where($where)->select();
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['name'];
            $temp['id'] = $v['id'];
            $temp["chkDisabled"]=false;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }

    //ztree产品树
    public function productTree(){
        $where = [];
        $keyword = $this->request->param('keyword');
        if($keyword){
            $series = Db::table('pro_series')->where('category_id',$keyword)->field('id')->select();
            foreach($series as $k=>$v){
                $series[$k] = $v['id'];
            }
            $series = implode(',',$series);
            $sku = Db::table('pro_sku')->where('category_id',$series)->field('id')->select();
            foreach($sku as $k=>$v){
                $sku[$k] = $v['id'];
            }
            $sku = implode(',',$sku);

            $where['sku_id'] = ['in',$sku];
        }
        $data = Db::table('pro_products')
            ->field('id,pro_name name,pro_no,unit')
            ->where($where)
            ->select();
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['name'];
            $temp['id'] = $v['id'];
            $temp["chkDisabled"]=false;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }

    //费用科目树
    public function feetype3(){
        $data = Db::table('cg_feiyongkemu1')->field('id,title name')->select();
        foreach($data as $k=>$v){
            $temp = Db::table('cg_feiyongkemu2')->field('id,title name')->where(array('pid'=>$v['id']))->select();
            foreach($temp as $k1=>$v1){
                $temp[$k1]['children'] = Db::table('cg_feiyongkemu3')->field('id,title name')->where(array('pid'=>$v1['id']))->select();
                $temp[$k1]["chkDisabled"]=false;
                $temp[$k1]["nocheck"]=true;
            }
            $data[$k]['children'] = $temp;
            $data[$k]["chkDisabled"]=true;
            $data[$k]["nocheck"]=true;
        }
        echo json_encode($data);
    }
    //费用计入部门树
    public function jrbmtree(){
        $data = Db::table('cg_fijiruzd t')->join('department t2','t2.id=t.title')->field('t2.de_name name,t.id')->select();
        echo json_encode($data);
    }
 public function bmtree(){
        $data = Db::table('department')->field('de_name name,id')->select();
        echo json_encode($data);
    }
    public function kaohetree(){
        $kaohe=db("cg_kaoheqi2 k2")->join("cg_kaoheqi1 k1","k2.pid=k1.id")->join("cg_cainian c","k1.cid=c.id")->order("k2.starttime desc")->field("k2.id,k2.title,c.title as cainian")->select();
        foreach ($kaohe as $k => $v) {
            # code...
            $data[$k]["id"]=$v["id"];
            $data[$k]["name"]=$v["cainian"].'-'.$v["title"];
        }
        echo json_encode($data);
    }
    //销售物料json
    public function materialjson(){
        $param = $this->request->param();
        //echo $param['id'];
        if(isset($param['id'])&&$param['id']==0){ //样品
            //用JOIN把5张表连起来获取数据更方便

            $getData = $this->request->param(); //type  style

            $sytle = 5;

            $type = 2;
            $retData = [];
            $retData2 = [];
            $where1=["is_actived"=>1,"is_deleted"=>0];
            $brands = Db::name('pro_brands')->where($where1)->field('id')->select();
            $i = 0;
            $j = 0;

            if($brands){
                Db::startTrans();
                try {

                    for($i=0;$i<count($brands);$i++){
                        $brands2[]=$brands[$i]["id"];
                    }

                    $brand_id=implode(',',$brands2);
                    $where2="brand_id in ($brand_id) ";
                    $category = Db::name("pro_category")
                        ->where($where1)->where($where2)->field('id')
                        ->select();
                    //if()

                    if($category){
                        for($i=0;$i<count($category);$i++){
                            $category2[]=$category[$i]["id"];
                        }

                        $category_id=implode(',',$category2);
                        $where3="category_id in ($category_id) ";
                        $pro_series = Db::name("pro_series")->where($where1)->where($where3)
                            ->field('series_name as name ,id')
                            ->select();
                    }

                    if($pro_series){
                        for($i=0;$i<count($pro_series);$i++){
                            $pro_series2[]=$pro_series[$i]["id"];
                        }

                        $series_id=implode(',',$pro_series2);
                        $where4="series_id in ($series_id) ";
                        $sku = Db::name("pro_sku")->where($where1)->where($where4)
                            ->field('sku_name,id')
                            ->select();
                    }

                    if($sku){
                        for($i=0;$i<count($sku);$i++){
                            $sku2[]=$sku[$i]["id"];
                        }

                        $sku_id=implode(',',$sku2);
                        $where5="sku_id in ($sku_id) ";
                        $product = Db::name("pro_products")->where($where1)->where($where5)
                            ->field('pro_name as name ,id')
                            ->select();


                        $product = Db::name("pro_products")->alias("p")
                            ->join("pro_price pp", "pp.pid=p.id") //pro_price
                            ->field('p.id,p.pro_name as title,p.unit,pp.factory')
                            // ->field('p.id,p.pro_name as name,p.box_num,p.unit,pp.factory,pp.box_factory,pp.box_retail_standard as xtxgj,pp.box_sell_standard as yls')//这个加factory 有问题的
                            ->where($where5)
                            ->where($where1)
                            ->select();
                        echo json_encode(['message'=>'ok','value'=>$product,'code'=>'200','redirect'=>'']);
                    }


                } catch (\Exception $e) {
                    Db::rollBack();
                    return $e->getMessage();
                    return json_encode(["wrong" => "系统繁忙请稍后再试", "aid" => ""]);
                }

            }

            /*$data = Db::table('pro_materials t')->field('t.material_name title,t.id,unit,factory')->where("m_type=2 and is_deleted=0 and is_actived=1 ")->select();
            echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);*/
        }elseif (isset($param['id'])&&$param['id']==1){ // 物料
            $data = Db::table('pro_materials t')->field('t.material_name title,t.id,unit,factory')->where("m_type=2 and is_deleted=0 and is_actived=1 ")->select();
            echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
        }else{
            $data = Db::table('pro_materials t')->field('t.material_name title,t.id,unit,factory')->where("m_type=2 and is_deleted=0 and is_actived=1 ")->select();
            echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
        }

    }

    //销售样品json
    public function samplejson(){
        //用JOIN把5张表连起来获取数据更方便
        /*$getData = $this->request->param();
        $sytle = 5;

        $type = 2;
        $retData = [];
        $retData2 = [];*/
        $where1=["is_actived"=>1,"is_deleted"=>0];
        $brands = Db::name('pro_brands')->where($where1)->field('id')->select();

    /*    $i = 0;
        $j = 0;*/

        if($brands){
            Db::startTrans();
            try {

                for($i=0;$i<count($brands);$i++){
                    $brands2[]=$brands[$i]["id"];
                }

                $brand_id=implode(',',$brands2);
                $where2="brand_id in ($brand_id) ";
                $category = Db::name("pro_category")
                    ->where($where1)->where($where2)->field('id')
                    ->select();
                if($category){
                    for($i=0;$i<count($category);$i++){
                        $category2[]=$category[$i]["id"];
                    }

                    $category_id=implode(',',$category2);
                    $where3="category_id in ($category_id) ";
                    $pro_series = Db::name("pro_series")->where($where1)->where($where3)
                        ->field('series_name as name ,id')
                        ->select();
                }

                if($pro_series){
                    for($i=0;$i<count($pro_series);$i++){
                        $pro_series2[]=$pro_series[$i]["id"];
                    }

                    $series_id=implode(',',$pro_series2);
                    $where4="series_id in ($series_id) ";
                    $sku = Db::name("pro_sku")->where($where1)->where($where4)
                        ->field('sku_name,id')
                        ->select();
                }

                if($sku){
                    for($i=0;$i<count($sku);$i++){
                        $sku2[]=$sku[$i]["id"];
                    }

                    $sku_id=implode(',',$sku2);
                    $where5="sku_id in ($sku_id) ";
                    /*$product = Db::name("pro_products")->where($where1)->where($where5)
                        ->field('pro_name as name ,id')
                        ->select();*/
                    $product = Db::name("pro_products")->alias("p")
                        ->join("pro_price pp", "pp.pid=p.id") //pro_price
                        ->field('p.id,p.pro_name as title,p.unit,pp.factory')
                        // ->field('p.id,p.pro_name as name,p.box_num,p.unit,pp.factory,pp.box_factory,pp.box_retail_standard as xtxgj,pp.box_sell_standard as yls')//这个加factory 有问题的
                        ->where($where5)
                        ->where($where1)
                        ->select();
                    echo json_encode(['message'=>'ok','value'=>$product,'code'=>'200','redirect'=>'']);
                }


            } catch (\Exception $e) {
                Db::rollBack();
                return $e->getMessage();
                return json_encode(["wrong" => "系统繁忙请稍后再试", "aid" => ""]);
            }

        }
       /* $data = Db::table('pro_materials t')->field('t.material_name title,t.id,unit,factory')->where("m_type=2 and is_deleted=0 and is_actived=1 ")->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);*/
    }

    public function zoning(){
        return $this->fetch();
    }
    //返回网点树数据-从分区开始
    public function zoningTreeAjax(){
        $data = Db::table('cg_market_zoning')
            ->where('isdel',0)
            ->field('id,title')
            ->select();//查分区
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['title'];
            $temp['id'] = $v['id'];
//            $temp["chkDisabled"]=true;
//            $temp["nocheck"]=true;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }
}
